import { _decorator, Button, Label, EditBox, Node } from 'cc';
import { BaseWindow } from '../../../core/runtime/UIFramework';

const { ccclass, property } = _decorator;

/**
 * 输入框组件
 */
@ccclass('InputBox')
export class InputBox extends BaseWindow {
    @property(Label)
    private titleLab: Label | null = null;

    @property(Label)
    private descLab: Label | null = null;

    @property(EditBox)
    private inputField: EditBox | null = null;

    @property(Button)
    private confirmBtn: Button | null = null;

    @property(Button)
    private cancelBtn: Button | null = null;

    private _confirmCallback: ((input: string) => void) | null = null;
    private _cancelCallback: (() => void) | null = null;

    init(): void {
        this._uiName = "InputBox";
    }

    onOpen(data?: any): void {
        super.onOpen(data);
        
        if (data) {
            if (this.titleLab) {
                this.titleLab.string = data.title || "请输入";
            }
            
            if (this.descLab) {
                this.descLab.string = data.description || "";
            }
            
            if (this.inputField) {
                this.inputField.string = data.defaultValue || "";
                if (data.placeholder) {
                    // this.inputField.placeholder = data.placeholder;
                }
            }
            
            this._confirmCallback = data.onConfirm || null;
            this._cancelCallback = data.onCancel || null;
        }
    }

    onClose(): void {
        super.onClose();
        this._confirmCallback = null;
        this._cancelCallback = null;
    }

    /**
     * 获取输入内容
     */
    public getInputText(): string {
        return this.inputField ? this.inputField.string : "";
    }

    /**
     * 确认按钮点击事件
     */
    private onConfirmBtnClick(): void {
        if (this._confirmCallback) {
            this._confirmCallback(this.getInputText());
        }
        this.close();
    }

    /**
     * 取消按钮点击事件
     */
    private onCancelBtnClick(): void {
        if (this._cancelCallback) {
            this._cancelCallback();
        }
        this.close();
    }
}